<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img style="width:50px ; border-radius:50% " src="../assets/xbk.jpg" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button  @click="blank"  type="info">点击退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px':'200px' ">
                <div class="toggle-button"  @click="toggleCollapse" >|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu
                :collapse="isCollapse"
                :collapse-transition="false"
                :router="true"
                :default-active="activePath"
                background-color="#333744"
                text-color="#fff"
                active-text-color="#409EFF"
                >
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="item in numeList" :key="item.id">
                        <!-- 一级菜单模版区域 -->
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconObj[item.id]"></i>
                            <!-- 文本 -->
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item 
                            v-for="subitem in item.children" 
                            :index="'/'+subitem.path" 
                            :key="subitem.id"
                            @click="seveNavState('/'+subitem.path)"
                        >
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{subitem.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容主体 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>

export default {
    name:"home",
    
    data() {
        return {
            // 左侧菜单栏数据
            numeList:[],
            iconObj:{
                "125":"iconfont  icon-yonghuguanli",
                "103":"iconfont  icon-dingdan",
                "101":"iconfont  icon-shangpin",
                "102":"iconfont  icon-quanxian",
                "145":"iconfont  icon-tubiaozhizuomoban-27"
            },
            // 侧边栏是否折叠
            isCollapse:false,
            // 保存的二级菜单激活状态
            activePath:""
        }
    },
    created(){
        this.getnuList()
        // 给data.activePath赋值
        this.activePath = window.sessionStorage.getItem("activePath")
    },
    methods:{
        blank(){
            window.sessionStorage.clear();
            this.$router.push("/login")
        },
        // 获取所有的菜单
        async getnuList(){
            const {data} =  await this.$http.get("menus")
            if(!data.meta.status == 200){
                this.$message.error(data.meta.msg)
            }else{
                this.numeList = data.data
            }
            console.log(data)
        },
        // 点击按钮切换、控制菜单的折叠与展开
        toggleCollapse(){
            this.isCollapse = !this.isCollapse
        },
        // 保存链接的激活状态
        seveNavState(activePath){
            window.sessionStorage.setItem("activePath",activePath)
            this.activePath = activePath
        }
    }
}
</script>

<style scoped lang="less">
    .home-container{
        height:100%
    }
    .el-header{
        background-color: #373d41;
        display:flex;
        justify-content:space-between;
        align-items: center;
        color: white;
        div{
            display: flex;
            align-items: center;
            span{
                padding-left: 20px;
            }
        }
    }
    .el-aside{
        background-color: #333744;
        .el-menu{
            border: none;
        }
    }
    .el-main{
        background-color: #eaedf1;
    }
    .iconfont{
        padding-right: 10px;
    }
    .toggle-button{
        background-color: #4a5064;
        text-align: center;
        line-height: 24px;
        color: white;
        font-size: 10px;
        // 间距
        letter-spacing: 0.2em;
        cursor: pointer;
    }
</style>